<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Book extends CI_Controller {
    
    private $data = array();

    public function  __construct() {
        parent::__construct();
        
        //check session
        if( !$this->session->userdata('isLoggedIn') ) {
            redirect(base_url().'admin', 'refresh');
        }

        //Load Helper and library
        $this->load->helper("url");
        $this->load->library("ci_smarty");
        $this->load->helper("text"); 
        $this->data['base_url'] = base_url();
        $this->data['root_url'] = $this->config->item('root_url');
        $this->load->library('pagination'); 

        //$this->output->enable_profiler(TRUE);
        
        //Load Model
        $this->load->Model("admin/Mbook");
        $this->load->Model("admin/Mimage");
        $this->load->Model("admin/Mauthor");
        $this->load->Model("admin/Mcategory");
        $this->load->Model("admin/Mcategory_book");

        $this->data['nameAdmin'] = $this->session->userdata('name');
        $this->data['main_selected_cate'] = 'book';
    }

    public function listBook($curPage = 1)
    {
        // config page
        $bookPageConfig['base_url']      = base_url("admin/list-book/");
        $bookPageConfig['total_rows']    = $this->Mbook->countBook();
        $bookPageConfig['per_page']      = 5;
        $bookPageConfig['uri_segment']   = 3;
        $bookPageConfig['cur_page']      = $curPage;

        $curOffset = $bookPageConfig['per_page'] * ($curPage - 1);

        $this->pagination->initialize($bookPageConfig);

        $this->data['pageList'] = $this->pagination->create_links();

        $this->data['listBook'] = $this->Mbook->getBookByLimit($bookPageConfig['per_page'],$curOffset);

        $this->data['sub_selected_cate'] = 'list-book';

        $this->ci_smarty->view('backend/list-book.tpl',$this->data);
    }
    
    public function addBook()
    {
        // load image
        $this->data['listImage'] = $this->Mimage->getImage();

        // load author
        $this->data['listAuthor'] = $this->Mauthor->getAuthor();

        // load Category
        $this->data['listCategory'] = $this->Mcategory->getCategoryAll();

        // process inssert
        if($this->input->post()){
            // upload image
            $image = $this->Mimage->uploadImage();

            $param = $this->input->post();

            // insert image
            if($image['0']['is_image'] == 1){
                $dataLagre = $this->input->post();
                $dataLagre['filename'] = $image['0']['file_name'];
                $dataLagre['width']    = $image['0']['image_width'];
                $dataLagre['height']   = $image['0']['image_height'];
                $dataLagre['type'] = 'L';

                $dataMedium = $this->input->post();
                $dataMedium['filename'] = $image['0']['file_name'];
                $dataMedium['width']    = $image['1']['width'];
                $dataMedium['height']   = $image['1']['height'];
                $dataMedium['type']     = 'M';
 
                $dataSmall = $this->input->post();
                $dataSmall['filename'] = $image['0']['file_name'];
                $dataSmall['width']    = $image['2']['width'];
                $dataSmall['height']   = $image['2']['height'];
                $dataSmall['type']     = 'S';

                $image_id = $this->Mimage->insertImageBook($dataLagre);
                $this->Mimage->insertImageBook($dataMedium);
                $this->Mimage->insertImageBook($dataSmall);
                $param['image_id'] = $image_id;
            }
            $book_id = $this->Mbook->insertBook($param);

            //insert category book
            $param['book_id'] = $book_id;
            $this->Mcategory_book->insertCategoryBook($param);  

            redirect(base_url()."admin/list-book.html"); 
        }

        $this->data['sub_selected_cate'] = 'add-book';

        $this->ci_smarty->view('backend/add-book.tpl',$this->data); 
    }

    public function updateBook($book_uname)
    {
        //load book 
        $this->data['book'] = $this->Mbook->getBookByUname($book_uname);
		
        // load image
        //$this->data['listImage'] = $this->Mimage->getImageByID();

        // load author
        $this->data['listAuthor'] = $this->Mauthor->getAuthor();

        // load Category
        $this->data['listCategory'] = $this->Mcategory->getCategoryAll();

        // precess update
        if($this->input->post()){
            // upload image
            $image = $this->Mimage->uploadImage();

            $param = $this->input->post();

            // insert image
            if($image['0']['is_image'] == 1){
                $dataLagre = $this->input->post();
                $dataLagre['filename'] = $image['0']['file_name'];
                $dataLagre['width']    = $image['0']['image_width'];
                $dataLagre['height']   = $image['0']['image_height'];
                $dataLagre['type'] = 'L';

                $dataMedium = $this->input->post();
                $dataMedium['filename'] = $image['0']['file_name'];
                $dataMedium['width']    = $image['1']['width'];
                $dataMedium['height']   = $image['1']['height'];
                $dataMedium['type']     = 'M';
 
                $dataSmall = $this->input->post();
                $dataSmall['filename'] = $image['0']['file_name'];
                $dataSmall['width']    = $image['2']['width'];
                $dataSmall['height']   = $image['2']['height'];
                $dataSmall['type']     = 'S';

                $image_id = $this->Mimage->insertImageBook($dataLagre);
                $this->Mimage->insertImageBook($dataMedium);
                $this->Mimage->insertImageBook($dataSmall);
                $param['image_id'] = $image_id;
            }
            
            $this->Mbook->updateBook($param); 
            
            $param['book_id'] = $param['id'];
            $this->Mcategory_book->updateCategoryBook($param);  
            
            redirect(base_url()."admin/list-book.html"); 
        }
       
        $this->ci_smarty->view('backend/update-book.tpl',$this->data); 
    }

    public function deleteBook($book_id)
    {
        $this->Mcategory_book->deteleCategoryBook($book_id);
        $this->Mbook->deteleBook($book_id); 
        redirect(base_url()."admin/list-book.html"); 
    }
}